package com.appwx.sdk.app;

import android.content.Context;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.appwx.sdk.R;

public class AppToast extends Toast {

	public static final boolean VISIBILITY = true;
	public static AppToast appToast = null;
	public static LayoutInflater inflate = null;

	public AppToast(Context context) {
		super(context);
	}

	/**
	 * 自定义layout消息提醒
	 * @param context
	 * @param text
	 * @param duration
	 * @return
	 */
	public static AppToast makeText(Context context, CharSequence text,
			int duration) {
		if (VISIBILITY) {
			appToast = new AppToast(context);

			inflate = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View v = inflate.inflate(R.layout.view_tips, null);
			TextView tv = (TextView) v.findViewById(R.id.tips_msg);
			tv.setText(text);

			appToast.setView(v);
			appToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			appToast.setDuration(duration);
			return appToast;
		} else {
			return null;
		}

	}

	/**
	 * 纯文本消息提醒Resources
	 * @param context
	 * @param resId
	 * @param duration
	 * @return
	 * @throws Resources.NotFoundException
	 */
	public static AppToast makeText(Context context, int resId, int duration)
			throws Resources.NotFoundException {
		if (VISIBILITY) {
			appToast = new AppToast(context);
			appToast.setText(context.getResources().getText(resId));
			appToast.setDuration(duration);
			return appToast;
		}else{
			return null;
		}
	}

	/**
	 * 纯文本消息提醒string
	 * @param context
	 * @param text
	 * @param duration
	 * @return
	 */
	public static AppToast makeText(Context context, String text,int duration) {
		if (VISIBILITY) {
			appToast = new AppToast(context);
			appToast.setText(text);
			appToast.setDuration(duration);
			return appToast;
		}else{
			return null;
		}
	}
	
	
	public void setIcon(int iconResId) {
		if (getView() == null) {
			throw new RuntimeException(
					"This Toast was not created with Toast.makeText()");
		}
		ImageView iv = (ImageView) getView().findViewById(R.id.tips_icon);
		if (iv == null) {
			throw new RuntimeException(
					"This Toast was not created with Toast.makeText()");
		}
		iv.setImageResource(iconResId);
	}

	@Override
	public void setText(CharSequence s) {
		if (getView() == null) {
			throw new RuntimeException(
					"This Toast was not created with Toast.makeText()");
		}
		TextView tv = (TextView) getView().findViewById(R.id.tips_msg);
		if (tv == null) {
			throw new RuntimeException(
					"This Toast was not created with Toast.makeText()");
		}
		tv.setText(s);
	}

}
